package model;

import java.awt.geom.Point2D;

import ctf.model.Side;

import utils.Point2D_DataPackage;

/**
 * A class to control the properties of the games flags.
 * @author N. Bhatia, A. Butt, N. Evans, C. Levesque, P. OKeefe
 *
 */
public class Flag extends GameEntity implements IPublishable{
	
	private Player carrier = null;
	private boolean flagIsVisible = true;
	
	public Flag() {
		super(true);
	}
	@Override
	public void update(int elapsedTimeInMillis) {
		if (this.carrier != null) {
			this.setPosition(carrier.getPosition());
		}
	}
	/**
	 * Ties the flag to a Player who has picked it up.  This ensures position updates
	 * will be consistent between carrier and the flag.
	 * @param carrier
	 */
	public void pickedUpByPlayer(Player carrier) {
		this.carrier = carrier;
	}
	
	/**
	 * Checks to see if the flag is not being carried
	 * @return true if flag is not being carried
	 */
	public boolean hasNoCarrier(){
		if(carrier == null){
			return true;
		} 
		return false;
	}
	
	/**
	 * Resets the flag object.
	 */
	public void reset() {
		this.carrier = null;
		this.setPosition(new Point2D.Double(0, 0));
	}
	
	/**
	 * See the flags visibility position, pass in true to set the visible position
	 * to actual position, pass in false and visible position will be set to 0,0
	 * @param visible 
	 */
	public void setVisibility(boolean visible){
		flagIsVisible = visible;
	}
	

	@Override
	public String publish(Side side){
		String flagString = new String();
		flagString = " (" + 1 + " ";
		Point2D_DataPackage position = new Point2D_DataPackage();
		if(flagIsVisible || side == this.getSide()){
			position.point = this.getPosition();
		} else {
			position.point = new Point2D.Double(0.0, 0.0);
		}
		flagString += position.encode() + ")";
		return flagString;

	}

	/**
	 * Decouples the flag from a carrier.  The position will no longer be update when the player moves.
	 */
	public void drop() {
		this.carrier = null;
	}
	@Override
	public double getRadius() {
		return 0.0;
	}
	
}
